<?php
/**
 * @annotate 后台企业管理
 * @group   Admin/Haiyin
 * @author   yachtxiao <fuck@fackyou.org>
 */
class CompanyAction extends BaseAction {
	protected $aVerify = array(
   	'index'
  	);//需要验证的方法
	public function company_list(){
		$group = $_SESSION['admin']['group'];
		if(empty($_POST['company_name'])){
			$company = D('Company')->select_company($group);
		}else{
			$company = D('Company')->select_company($group,$_POST['company_name']);
		}
		$this->assign('company',$company);
		$this->display();
	}
	public function company_info_list(){
		$timetoday = strtotime(date("Y-m-d",time()));//今天0点的时间点
		$timeyestoday = $timetoday - 3600*24;//今天0点的时间点，两个值之间即为昨
		$cname = $_POST['company_name'];
		if($cname!=""){
			$arr['company_name'] = array('like',''.$cname.'%');
			$result = D('company')->where($arr)->getField('cid',true);
			$map['cid'] = array('in',$result);
		}else{
			$status = $_GET['status'];
			if($status == "nopass"){
				$map['status'] = 0;
				$map['is_del'] = 0;
			}
			$day = $_GET['day'];
			if($day=='today'){
				$map['updateline'] = array('egt',$timetoday);
			}elseif($day=='yesterday'){
				$map['updateline'] = array('between',$timeyestoday.','.$timetoday);
			}
		}
		$company = D('Company')->select_company_info($map);
		$this->assign('company',$company);
		$this->display();
	}
	public function company_add(){
		$pro = getarea();
		$menu = D('Menu')->select_menu();
		$agent = D('User')->get_user_bygroup(3);
		$this->assign('agent',$agent);
		$this->assign('menu',$menu);
		$this->assign('pro',$pro);
		$this->display();
	}
	public function company_doadd(){
		$this->assign('Title',"人才来了添加企业");
		$url = U('Company/company_list');
		$data['company_agent'] = $_POST['company_agent'];
		$data['company_name'] = $_POST['company_name'];
		$data['company_admin'] = $_POST['company_admin'];
		$data['company_salt'] = rand(100000,999999);
		$data['company_pwd'] = md5(md5($_POST['company_pwd']).$data['company_salt']);
		$data['company_area'] = json_encode(array('pro'=>$_POST['pro'],'city'=>$_POST['city'],'area'=>$_POST['area']));
		$data['company_menu'] = $_POST['company_menu'];
		$data['ctime'] = time();
		$data['updateline'] = time();
		$cid = D('company')->add($data);
		if($cid){
			D('company_info')->add(array('cid'=>$cid));
			$me = D('menu')->where('mid ='. $_POST['company_menu'])->find();
			$me['cid']=$cid;
			$me['ctime'] = time();
			$me['update_time'] = time();
			D('company_menu')->add($me);
			$this->assign("jumpUrl",$url);//跳转
        	$this->success("添加成功！");
		}	
	}
	public function company_edit(){
		$pro = getarea();
		$company = D('Company')->get_company($_GET['cid']);
		$agent = D('User')->get_user_bygroup(3);
		$menu = D('Menu')->select_menu();
		$this->assign('menu',$menu);
		$this->assign('agent',$agent);
		$this->assign('company',$company);
		$this->assign('pro',$pro);
		$this->display();
	}
	public function company_doedit(){
		$this->assign('Title',"人才来了修改企业");
		$url = U('Company/company_list');
		$data['company_agent'] = $_POST['company_agent'];
		$data['company_name'] = $_POST['company_name'];
		$data['company_admin'] = $_POST['company_admin'];
		if(!empty($_POST['company_pwd'])){
			$data['company_salt'] = rand(100000,999999);
			$data['company_pwd'] = md5(md5($_POST['company_pwd']).$data['company_salt']);
		}
		$data['company_area'] = json_encode(array('pro'=>$_POST['pro'],'city'=>$_POST['city'],'area'=>$_POST['area']));
		$data['company_menu'] = $_POST['company_menu'];
		$data['updateline'] = time();
		if(D('company')->where('cid = '.$_POST['cid'])->save($data)){
			$me = D('menu')->where('mid ='. $_POST['company_menu'])->find();
			$menu = D('company_menu')->where('cid = '.$_POST['cid'])->find();
			if($menu ==""){
				$me['ctime'] = time();
				$me['update_time'] = time();
				$me['cid']=$_POST['cid'];
				D('company_menu')->add($me);
			}else{
				if($menu['mid'] == $_POST['company_menu']){
					$me['ctime'] = $menu['ctime'];
					$me['update_time'] = time();
					$menu1 = D('company_menu')->where('cid = '.$_POST['cid'])->save($me);
				}else{
					$me['ctime'] = time();
					$me['update_time'] = time();
					$menu1 = D('company_menu')->where('cid = '.$_POST['cid'])->save($me);
				}
				
			}
			
			$this->assign("jumpUrl",$url);//跳转
        	$this->success("修改成功！");
		}	
	}
	public function company_job_list(){
		$this->assign('Title',"人才来了职位管理");
		$timetoday = strtotime(date("Y-m-d",time()));//今天0点的时间点
		$timeyestoday = $timetoday - 3600*24;//今天0点的时间点，两个值之间即为昨
		$group = $_SESSION['admin']['group'];
		$map['is_del'] = 0;
		$day = $_GET['day'];
		if($day=='today'){
			$map['updateline'] = array('egt',$timetoday);
		}elseif($day=='yesterday'){
			$map['updateline'] = array('between',$timeyestoday.','.$timetoday);
		}
		if(empty($_POST['company_name'])){
			$hirings = D('Company')->get_company_jobs_list($group,$map);
		}else{
			$hirings = D('Company')->get_company_jobs_list_bykey($_POST['company_name'],$group,$map);
		}
		$this->assign('hirings',$hirings);
		$this->display();
	}
	public function company_job_list_nopass(){
		$this->assign('Title',"人才来了职位管理");
		$group = $_SESSION['admin']['group'];
		$map['status'] = 0;
		$map['is_del'] = 0;
		if(empty($_POST['company_name'])){
			$hirings = D('Company')->get_company_jobs_list($group,$map);
		}else{
			$hirings = D('Company')->get_company_jobs_list_bykey($_POST['company_name'],$group,$map);
		}
		$this->assign('hirings',$hirings);
		$this->display();
	}
	public function getarea(){
		$type = $_POST['type'];
		$aid = $_POST['aid'];
		$area  = getarea($type,$aid);
		exit(json_encode($area));
	}
	public function del_jobs(){
		$map['hiring_id'] = $_POST['id'];
		$data['is_del'] = 1;
		$da = D('hiring')->where($map)->save($data);
		if($da){
			$this->success("删除成功！",'__URL__/company_job_list');    
		} 
	}
	public function del_user(){
		$map['cid'] = $_POST['id'];
		$data['is_del'] = 1;
		$da = D('company')->where($map)->save($data);
		if($da){
			$this->success("删除成功！",'__URL__/company_list');    
		} 
	}
	public function del_company_info(){
		$map['cid'] = $_POST['id'];
		$data['is_del'] = 1;
		$da = D('company_info')->where($map)->save($data);
		if($da){
			$this->success("删除成功！",'__URL__/company_info_list');    
		} 
	}
	public function edit_company_status(){
		$map['cid'] = $_POST['id'];
		$data['status'] = $_POST['val'];
		$da = D('company_info')->where($map)->save($data);
		if($da){
			$this->success("更新成功！",'__URL__/company_info_list');    
		} 
	}
	public function edit_hiring_status(){
		$map['hiring_id'] = $_POST['id'];
		$data['status'] = $_POST['val'];
		$da = D('hiring')->where($map)->save($data);
		if($da){
			$this->success("更新成功！",'__URL__/company_job_list');    
		} 
	}
	public function company_job_edit(){
		$hid = $_GET['hiring_id'];
		$cid = D('hiring')->where(array('hiring_id'=>$hid))->find();
		$cid = $cid['cid'];
		$this->assign('cid',$cid);
		$this->assign("Title","修改职位");
		$company_info = D('Company')->getinfo($cid);
		$stores = D('Company')->list_store($cid);
		$list = D('Hiring')->get_hiring_list($hid);
		$manage = D('Company')->get_manage_list($cid);
		$this->assign('manage',$manage);
		$this->assign('list',$list);
		$this->assign('hangye',gethangye());
		$this->assign('edu',getedu());
		$this->assign('height',C('HEIGHT_CONFIG'));
		$this->assign('exper',C('EXPER_CONFIG'));
		$this->assign('worktime',getworktime());
		$this->assign('workplace',format_workplace($stores));
		$templates = D('Hiring')->get_hiring_template($cid);
		$this->assign('templates',$templates);
		$position = D('position')->where('is_del = 0 AND hid = '.$list['hid'])->select();
		$this->assign('posi',$position);
		$this->assign('brand',format_brand($company_info['brand']));
		$this->display();
	
	}

	public function save_hiring(){
		$hiring_id = $_GET['hiring_id'];
		$data = $_POST;
		$data['info']['notice'] = implode(',', $data['info']['notice']);
		$result_hiring = D('Hiring')->save_hiring($data,$hiring_id);
		$result_hiring_info = D('Hiring')->save_hiring_info($data['info'],$hiring_id);
		if($result_hiring&&$result_hiring_info){
			$this->success("修改成功！",'__URL__/company_job_list');	
		}else{
			$this->error("修改失败！");	
		}
	}
	public function edit_menu(){
		$cid = $_GET['cid'];
		$list = D('company_menu')->where('cid ='.$cid)->find();
		$this->assign('list',$list);
		if ($this->isPost()){
			$data = $_POST;
			$lt = D('company_menu')->where('cid ='.$data['cid'])->find();
			if($lt != ""){
				$result = D('company_menu')->where('cid ='.$data['cid'])->save($data);
			}else{
				$result = D('company_menu')->add($data);
			}
			if($result){
				$this->success('修改成功！','__URL__/company_list');
			}else{
				$this->error('修改失败！');
			}
		}
		$this->display();
		
	}
}
?>